<?php

class UserFeedIndex extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/*
	 * @ 覆盖主键
	 */
	public function primaryKey()
	{
		return array('user_id','event_id');
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user_feed_index}}';
	}
	
	/*
	 * @ 重新relations
	 *   映射用户表
	 */
	public function relations()
	{
		return array(
			'users' => array(self::BELONGS_TO, 'Users', 'user_id'),
			'eventinfo' => array(self::BELONGS_TO, 'FeedList', 'event_id'),
			'eventparent' => array(self::BELONGS_TO, 'FeedRelation', 'event_id')
		);
	}

	/*
	 * @插入索引
	 */
	public function addFeedIndex($event_id,$create_time,$type)
	{
		//循环给好友发送
		$this->setIsNewRecord(true);
		$this->user_id = Yii::app()->user->getId();
		$this->event_id = $event_id;
		$this->type = $type;
		$this->create_time = $create_time;
		self::model()->save();
		
		$users = Users::model()->findByPk(Yii::app()->user->getId());
	
		foreach ($users->friendship as $friend)
		{
			$this->setIsNewRecord(true);
			$this->user_id = $friend->friend_id;
			$this->event_id = $event_id;
			$this->type = $type;
			$this->create_time = $create_time;
			self::model()->save();
		}
	}
}